<?php
namespace app\index\logic;
use think\Db;

class ReadHistory
{
    /*
     * 阅读历史删除
     * */
    public function readDel($params)
    {
        $where[] = ['novel_id','in',$params['nid']];
        $where[] = ['uid','=',request()->userInfo['id']];

        $ReadHistoryModel = \think\facade\App::model('ReadHistory', 'model');
        $result = $ReadHistoryModel->deleteRecord($where);

        if($result)
        {
            return ['code' => 0, 'msg' => 'success','data'=>[]];
        }

        return ['code' => 102, 'msg' => '阅读数据删除失败'];
    }

    /*
     * 获取用户最近阅读记录
     * */
    public function getRecentRecord($uid)
    {
        $field = 'r.novel_id as nid,r.chapter_no as chapnum,n.origin_title as bookname';

        $where[] = ['r.uid','=',$uid];
        $where[] = ['n.in_shelf','=',1];
        $orderBy = [ 'r.read_time'=>'DESC', 'r.id' => 'DESC'];

        $record = Db::table('quick_user_bookshelf')->alias('r')->join('__NOVEL__ n','n.id=r.novel_id')->where($where)
            ->order($orderBy)->field($field)->findOrEmpty();

        if(empty($record))
        {
            return $record;
        }

        $record['chapter_title'] = '第'.$record['chapnum'].'章';
        return $record;
    }

    /*
     * 阅读历史列表
     * */
    public function getList($params)
    {
        $params['page']     = isset($params['Pages']) ? $params['Pages'] : 1;
        $params['limit']    = isset($params['Size']) ? $params['Size'] : 20;

        $where[] = ['uid','=',request()->userInfo['id']];
        $orderBy = [ 'read_time'=>'DESC', 'id' => 'DESC'];

        $ReadHistoryModel = \think\facade\App::model('ReadHistory', 'model');
        $list = $ReadHistoryModel->getList($where,'novel_id AS nid,chapter_no,read_time',$params['page'],$params['limit'],$orderBy);

        $data = [];

        if($list)
        {
            $nid_gh = array_column($list,'nid');
            $NovelLogic = \think\facade\App::model('Novel', 'logic');
            $ChapterLogic = \think\facade\App::model('Chapter', 'logic');
            $novels = $NovelLogic->getListById($nid_gh);

            foreach ($list as $k=>$v)
            {
                if(!isset($novels[$v['nid']]))
                {
                    continue;
                }

                $chapterInfo = $ChapterLogic->getMaxChapnumInfo($v['nid']);

                $data[$k]['nid']            = $v['nid'];
                $data[$k]['chapnum_recent'] = $chapterInfo ? $chapterInfo['chapter_no'] : 1;
                $data[$k]['chapter_recent'] = "第".$data[$k]['chapnum_recent']."章";
                $data[$k]['chapnum_read']   = $v['chapter_no'];
                $data[$k]['chapter_read']   = '第'.$v['chapter_no'].'章';
                $data[$k]['bookname']       = $novels[$v['nid']]['bookname'];
                $data[$k]['update_status']  = $novels[$v['nid']]['update_status'];
                $data[$k]['vertical_cover'] = strlen($novels[$v['nid']]['cover']) > 1 ? $novels[$v['nid']]['cover'] : '';
                $data[$k]['read_time']      = $v['read_time'];

            }
        }

        $result['total']    = $ReadHistoryModel->getTotalNumber($where);
        $result['list']     = $data ? array_values($data) : [];

        return $result;
    }

}